package com.example.calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0; 
	Button btnPlus, btnEqual, btnClear;
	
	
	int result, operand1, operand2;
	
	TextView tv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tv = (TextView)findViewById(R.id.textView);
		tv.setText("Hey~");
		
		btn1 = (Button)findViewById(R.id.button1);
		btn2 = (Button)findViewById(R.id.button2);
		btn3 = (Button)findViewById(R.id.button3);
		btn4 = (Button)findViewById(R.id.button4);
		btn5 = (Button)findViewById(R.id.button5);
		btn6 = (Button)findViewById(R.id.button6);
		btn7 = (Button)findViewById(R.id.button7);
		btn8 = (Button)findViewById(R.id.button8);
		btn9 = (Button)findViewById(R.id.button9);
		btn0 = (Button)findViewById(R.id.button0);
		btnPlus = (Button)findViewById(R.id.buttonplus);
		btnEqual = (Button)findViewById(R.id.buttonequal);
		btnClear = (Button)findViewById(R.id.buttonclear);
		
//		btn1.setOnClickListener(new Button.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				//tv.setText("1 clicked~");
//				tv.setText(btn8.getText());
//			}
//		});
		
		result = 0;
		
		
		btn1.setOnClickListener(mClickListener);
		btn2.setOnClickListener(mClickListener);
		btn3.setOnClickListener(mClickListener);
		btn4.setOnClickListener(mClickListener);
		btn5.setOnClickListener(mClickListener);
		btn6.setOnClickListener(mClickListener);
		btn7.setOnClickListener(mClickListener);
		btn8.setOnClickListener(mClickListener);
		btn9.setOnClickListener(mClickListener);
		btn0.setOnClickListener(mClickListener);
		btnPlus.setOnClickListener(mClickListener);
		btnEqual.setOnClickListener(mClickListener);
		btnClear.setOnClickListener(mClickListener);
		

	}
	
	Button.OnClickListener mClickListener = new Button.OnClickListener()
	{
		public void onClick(View v)
		{
			switch(v.getId())
			{

				case R.id.buttonclear :
					result = 0;
					operand1 = 0;
					operand2 = 0;
					tv.setText("");
					break;
			
				case R.id.buttonplus :
					//tv.setText("++++++");
					operand1 = result;
					result = 0;
					break;
					
				case R.id.buttonequal :
					//tv.setText("======");
					operand2 = result;
					result = operand1 + operand2;
					tv.setText(String.valueOf(result));
					break;
				
				case R.id.button1 :
					result = result*10 + 1;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button2 :
					result = result*10 + 2;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button3 :
					result = result*10 + 3;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button4 :
					result = result*10 + 4;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button5 :
					result = result*10 + 5;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button6 :
					result = result*10 + 6;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button7 :
					result = result*10 + 7;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button8 :
					result = result*10 + 8;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button9 :
					result = result*10 + 9;
					tv.setText(String.valueOf(result));
					break;
				case R.id.button0 :
					result = result*10 + 0;
					tv.setText(String.valueOf(result));
					break;
					
			}
		}
	};
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
